package game.web.base;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import projectx.core.base.ProjectXSystemException;
import projectx.core.base.ProjectXUserException;
import projectx.db.ProjectXSQLException;
import projectx.transaction.DatabaseTransaction;
import projectx.transaction.TransactionHandler;

public class UserGroup {

	// --------------------------------------------------------------------------
	// S T A T I C M E M B E R S
	//
	// --------------------------------------------------------------------------
	private static final Logger _log = Logger.getLogger(UserGroup.class
			.getName());

	private static boolean _initialized;
	private static String[] _id;
	private static Map _map;

	// --------------------------------------------------------------------------
	// S T A T I C M E T H O D S
	//
	// --------------------------------------------------------------------------

	public static void initialize() throws ProjectXUserException,
			ProjectXSystemException {

		if (!_initialized) {

			synchronized (UserGroup.class) {

				if (!_initialized) {

					_log.info("Load T_USER_GROUP...");

					//TransactionHandler.call(new LoadUserGroupTrx());

					_log.info("Initialized successfully");

					_initialized = true;
				}
			}
		}
	}

	public static boolean isInitialized() {
		return _initialized;
	}

	public static String[] getId() {
		return _id;
	}

	public static String getName(String id) {
		return ((UserGroupInfo) _map.get(id)).getUserGroupName();
	}

	public static String[] getPrivilegeName(String id) {
		return ((UserGroupInfo) _map.get(id)).getPrivilegeName();
	}

	// --------------------------------------------------------------------------
	// C O N S T R U C T O R S
	//
	// --------------------------------------------------------------------------
	private UserGroup() {
	}

	// --------------------------------------------------------------------------
	// I N N E R C L A S S
	//
	// --------------------------------------------------------------------------
	private static class UserGroupInfo {

		private final String _userGroupName;
		private String[] _privilegeName;

		private UserGroupInfo(String userGroupName) {

			_userGroupName = userGroupName;
		}

		private void setPrivilegeName(String[] privilegeName) {
			_privilegeName = privilegeName;
		}

		private String getUserGroupName() {
			return _userGroupName;
		}

		private String[] getPrivilegeName() {
			return _privilegeName;
		}
	}

	private static class LoadUserGroupTrx extends DatabaseTransaction {

		public Object execute() throws ProjectXUserException,
				ProjectXSystemException {

			String sql = "SELECT user_group_id, user_group_name FROM t_user_group";// ORDER
																					// BY 2
																					// FOR
																					// READ
																					// ONLY

			PreparedStatement ps = null;
			ResultSet rs = null;

			try {

				if (_log.isDebugEnabled()) {
					_log.debug(sql);
				}

				ps = _conn.prepareStatement(sql);
				rs = ps.executeQuery();

				List list = new ArrayList();

				_map = new HashMap();

				while (rs.next()) {

					String userGroupId = rs.getString(1).trim();
					UserGroupInfo info = new UserGroupInfo(rs.getString(2)
							.trim());

					info.setPrivilegeName(getPrivilegeName(userGroupId));

					list.add(userGroupId);
					_map.put(userGroupId, info);
				}

				_id = (String[]) list.toArray(new String[list.size()]);

				return null;
			} catch (SQLException e) {
				_log.error(sql, e);
				throw new ProjectXSQLException(e);
			} finally {
				if (rs != null)
					try {
						rs.close();
					} catch (SQLException e) {
					}
				if (ps != null)
					try {
						ps.close();
					} catch (SQLException e) {
					}
			}
		}

		private String[] getPrivilegeName(String userGroupId)
				throws ProjectXSQLException {

			String sql = "SELECT privilege_name FROM t_privilege "
					+ "WHERE privilege IN ( SELECT privilege FROM t_group_priv WHERE user_group_id = ? )";

			PreparedStatement ps = null;
			ResultSet rs = null;

			try {

				if (_log.isDebugEnabled()) {
					_log.debug(sql);
				}

				ps = _conn.prepareStatement(sql);
				ps.setString(1, userGroupId);

				rs = ps.executeQuery();

				List ret = new ArrayList();

				while (rs.next()) {
					ret.add(rs.getString(1).trim());
				}

				return (String[]) ret.toArray(new String[ret.size()]);
			} catch (SQLException e) {
				_log.error(sql, e);
				throw new ProjectXSQLException(e);
			} finally {
				if (rs != null)
					try {
						rs.close();
					} catch (SQLException e) {
					}
				if (ps != null)
					try {
						ps.close();
					} catch (SQLException e) {
					}
			}
		}
	}
}
